package com.xebest.web.service.impl;

import com.xebest.common.enums.erp.order.VenOrderLogisticsEnum;
import com.xebest.common.enums.erp.order.VenOrderPayStatusEnum;
import com.xebest.common.enums.erp.order.VenOrderStatusEnum;
import com.xebest.web.form.XeVenOrderCreditcardRecordsForm;
import com.xebest.web.mapper.read.XeVenOrderCreditCardRecordsReadMapper;
import com.xebest.web.mapper.write.XeVenOrderCreditCardRecordsMapper;
import com.xebest.web.model.XeVenOrderCreditCardRecordsEntity;
import com.xebest.web.model.XeVenOrderInfoEntity;
import com.xebest.web.model.XeVenOrderLogistics;
import com.xebest.web.service.XeVenOrderCreditcardRecordsService;
import com.xebest.web.service.XeVenOrderService;
import com.xebest.web.service.XeVenSendGoodsService;
import com.xebest.web.view.XeVenOrderCreditCardRecordsView;
import com.xebest.web.view.XeVenSendView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: chenjing
 * @createTime: 2017/8/28 16:54
 * @history:
 * @version:v1.0
 */
@Service
public class XeVenOrderCreditcardRecordsServiceImpl implements XeVenOrderCreditcardRecordsService {

    private static Logger log = LoggerFactory.getLogger(XeVenAfterSaleServiceImpl.class);

    @Autowired
    private XeVenOrderCreditCardRecordsMapper xeVenOrderCreditCardRecordsMapper;

    @Autowired
    private XeVenOrderCreditCardRecordsReadMapper xeVenOrderCreditCardRecordsReadMapper;

    @Autowired
    private XeVenSendGoodsService xeVenSendGoodsService;

    @Autowired
    private XeVenOrderService xeVenOrderService;

    /**
     * @Author:chenjing
     * @Description: 插入pos支付成功回调记录
     * @Date:16:55 2017/8/28
     * @params: * @param null
     * @return:
     */
    @Override
    @Transactional(value="writeTransactionManager")
    public int insertRecords(XeVenOrderCreditCardRecordsEntity recordsEntity, XeVenOrderInfoEntity entity)  throws Exception{
        int flag = 0;
        List<String> list = new ArrayList<>();
        String orderId = recordsEntity.getOrderId();
        list.add(orderId);
        String userCode = entity.getUserCode();
        //如果订单状态为待发货，需要调用发货接口
        if(VenOrderStatusEnum.WAIT_SEND.getValue().equals(entity.getOrderStatus())){
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("userCode", userCode);//商户编码
            //paramMap.put("userCode", "SH170817899376");//商户编码
            paramMap.put("orderIds", list);//待发货订单号
            XeVenSendView sendView = new XeVenSendView();
            //批量查询要处理的待发货订单
            List<XeVenSendView> waitSendList = xeVenSendGoodsService.queryWaitSendList(paramMap);
            for(XeVenSendView view:waitSendList){
                if(view.getOrderId().equals(entity.getOrderId())){
                    sendView = view;
                    break;
                }
            }
            flag = xeVenSendGoodsService.updateSend(sendView);
            if(flag==0){
                log.error("订单发货失败，orderId={}",orderId);
                throw new RuntimeException("订单发货失败,orderId="+orderId);
            }
        }
        entity.setPayTime(recordsEntity.getPayTime());
        entity.setPayNumber(recordsEntity.getPayNumber());
        entity.setPayStatus(VenOrderPayStatusEnum.ALL_PAID.getValue());
        entity.setOrderStatus(VenOrderStatusEnum.FINISH.getValue());
        //更新订单状态=已完成
        flag = xeVenOrderCreditCardRecordsMapper.updateOrderInfo(entity);
        if(flag==0){
            log.error("更新订单状态失败，orderId={}",orderId);
            throw new RuntimeException("更新订单状态(已完成)失败!,orderId="+orderId);
        }
        //插入pos支付成功记录表
         flag = xeVenOrderCreditCardRecordsMapper.insertRecords(recordsEntity);
        if(flag==0){
            log.error("插入pos记录表失败，{}",recordsEntity);
            throw new RuntimeException("插入pos记录表失败");
        }
        //插入物流信息表
        XeVenOrderLogistics logt = new XeVenOrderLogistics();
        logt.setLogtsStatus(VenOrderLogisticsEnum.SIGN.getValue());
        logt.setUserCode(userCode);
        logt.setOrderId(orderId);
        flag = xeVenOrderService.updateOrderLogistics(logt,null);
        if(flag==0){
            log.error("插入物流信息表失败，{}",logt);
            throw new RuntimeException("插入物流信息表失败!");
        }
        return flag;
    }

    /**
     * @Author:chenjing
     * @Description:查询订单信息
     * @Date:17:41 2017/8/28
     * @params: * @param null
     * @return:
     */
    @Override
    public XeVenOrderInfoEntity queryOrderInfo(String orderId) {
        return xeVenOrderCreditCardRecordsMapper.queryOrderInfo(orderId);
    }

    /**
     * @Author:chenjing
     * @Description:查询pos刷卡记录
     * @Date:14:48 2017/10/16
     * @params: * @param null
     * @return:
     */
    @Override
    public List<XeVenOrderCreditCardRecordsView> queryCreditCardRecordsList(XeVenOrderCreditcardRecordsForm form) {
        return xeVenOrderCreditCardRecordsReadMapper.queryCreditCardRecordsList(form);
    }
}
